const Router = require('koa-router')
const collected= new Router()
//！拿到前端的数据
//数据库连接池
const pg = require('pg');
let psql = new pg.Pool({
    database: 'running',
    user: 'run',
    port: 5432,
    host: '82.157.94.115',
    password: 'z12345678',
    max: 2
})

collected.post('/collected', async ctx => {
    // ctx.body = ''
    let { content_id, content_title, content_content, content_time, content_img} = ctx.request.body;
    console.log("content_id:", content_id);
    console.log("content_title:", content_title);
    console.log("content_content:", content_content);
    console.log("content_time:", content_time);
    console.log("content_img:", content_img);
    let insertContentPostgresql = `insert into collectArticles (content_id,content_title,content_content,content_time,content_img) values ('${content_id}','${content_title}', '${content_content}', '${content_time}, '${content_img}')`;
    let content = await psql.query(insertContentPostgresql);
})



// collectArticles.get('/collectArticles', async ctx => {
//     let searchContentPostgresql = `select * from content`;
//     let content = await psql.query(searchContentPostgresql);
//     // console.log(content)
//     // console.log("行数", content.rowCount)

//     if (content.rowCount > 0) {
//         var collectArticlesObj = [];
//         for (var i = 0; i < content.rowCount; i++) {
//             var obj = {
//                 content_id: content.rows[i].content_id,
//                 content_title: content.rows[i].content_title,
//                 content_content: content.rows[i].content_content,
//                 content_time: content.rows[i].content_time,
//                 content_img: content.rows[i].content_img,
//                 collected:content.rows[i].collected
//             }

//             collectArticlesObj.push(obj);
//         }

//         ctx.body = {
//             collectArticlesObj
//         };
//     }

// })
module.exports = collected;